<?php

namespace app\beautiful\controller\miniapp\member;

use app\BaseController;

use app\beautiful\model\LookLikesModel;

use app\beautiful\model\LookModel;

use app\beautiful\model\mall\GoodsModel;



class Look extends BaseController{

    public function getLookList()

    {

        $where=[

            ['shop_id','=',$this->request->shopId],

            ['is_delete','=',0],

        ];

        $datas=LookModel::withCount(['likes'=>function($query){

            $query->where([

                ['member_id','=',$this->request->memberId],

                ['shop_id','=',$this->request->shopId]

            ]);

        }])->where($where)

            ->order(['update_time'=>'desc'])

            ->field('look_id,photos,info,see_num,likes_num,add_time')

            ->limit($this->request->limit)

            ->page($this->request->page)

            ->select()

            ->append(['add_time_format'])

            ->toArray();

        $saveAll =  [];

        foreach($datas as $val){

            $saveAll[] = [

                'look_id' => $val['look_id'],

                'see_num' => $val['see_num'] + 1,

            ];

      

        }

       if(!empty($saveAll)){

            $LookModel = new LookModel();

            $LookModel->saveAll($saveAll);

        }

        $return['datas']=$datas;

        return $this->success($return);

    }



    



    public function likesLook()

    {

        $look_id = $this->request->param('look_id/d',0);



        if(null === $look = LookModel::find($look_id)) return $this->error('相册不见了');

        if($look->shop_id != $this->request->shopId) return $this->error('相册不见了');

        if($look->is_delete == 1) return $this->error('相册不见了');



        if(null !== LookLikesModel::where([

                ['shop_id','=',$this->request->shopId],

                ['member_id','=',$this->request->memberId],

                ['look_id','=',$look_id]

            ])->find()){

            return $this->error('您已经点过赞了,请勿重复操作');

        }



        $look_likes = new LookLikesModel();

        $look_likes->shop_id=$this->request->shopId;

        $look_likes->member_id=$this->request->memberId;

        $look_likes->look_id=$look_id;

        $look_likes->save();



        $look->likes_num = $look->likes_num + 1;

        $look->save();



        return $this->success('点赞成功');

    }

}